import { Record, List } from 'immutable';
import Param from './param';
import { RESPONSE_TYPE } from '../constant';
import generateId from '../../utils/generate-id';

const DEFAULTS = {
  id: undefined,
  name: undefined,
  url: undefined,
  params: undefined,
  responseType: RESPONSE_TYPE.OBJECT,
  recordType: undefined,
  keylist: [],
  keys: [],
  method: 'GET',
};
export default class APIProvider extends Record(DEFAULTS) {
  static create(attr = {}) {
    const {
      name,
      url,
      params = [],
      recordType,
      keylist,
      method,
      keys = [],
      id = generateId(),
    } = attr;
    return new APIProvider({
      name,
      url,
      keylist,
      params: Param.createList(params),
      recordType,
      id,
      keys,
      method,
    });
  }

  static createList(attr = []) {
    if (List.isList(attr) || Array.isArray(attr)) {
      return List(attr.map(APIProvider.create));
    }
  }

  hasParam() {
    return this.params.size > 0;
  }
}
